package com.example.iotmanagerment26.mapper;

import com.example.iotmanagerment26.domain.Room;
import com.example.iotmanagerment26.domain.RoomDevice;
import com.example.iotmanagerment26.domain.RoomUser;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface RoomMapper {

    @Select("select * from room")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(
                    property = "roomDevice",  //实体类的字段
                    column = "name",   //selectRoomDeviceByName的字段名
                    many = @Many(select = "com.example.iotmanagerment26.mapper.RoomMapper.selectRoomDeviceByName")
            ),
            @Result(
                    property = "roomUser",
                    column = "name",
                    many = @Many(select = "com.example.iotmanagerment26.mapper.RoomMapper.selectRoomUserByName")
            )
    })
    List<Room> selectAllRoom();

    @Select("select * from room where name=#{roomName}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(
                    property = "roomDevice",
                    column = "name",
                    many = @Many(select = "com.example.iotmanagerment26.mapper.RoomMapper.selectRoomDeviceByName")
            ),
            @Result(
                    property = "roomUser",
                    column = "name",
                    many = @Many(select = "com.example.iotmanagerment26.mapper.RoomMapper.selectRoomUserByName")
            )
    })
    List<Room> selectRoomInfoByName(String roomName);

    @Select("select * from room_device where name=#{name}")
    List<RoomDevice> selectRoomDeviceByName(String name);

    @Select("select * from room_user where name=#{name}")
    List<RoomUser> selectRoomUserByName(String name);

    @Insert("insert into room values (null,#{name})")
    //@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insertRoom(String name);

    @Delete("delete from room where name=#{name}")
    int deleteRoom(String name);

    @Delete("delete from room_device where name=#{name}")
    int deleteRoomDevice2(String name);

    @Delete("delete from room_user where name=#{name}")
    int deleteRoomUser2(String name);

    //0=不存在
    @Select("select COUNT(*) from room where name=#{name}")
    int isEmptyRoom(String name);

    @Select("select COUNT(*) from room_device where name=#{name} and devid=#{device}")
    int isEmptyRoomDevice(String name, String device);

    @Select("select COUNT(*) from room_user where name=#{name} and user=#{user}")
    int isEmptyRoomUser(String name, String user);

    @Insert("insert into room_device values (null,#{name},#{device})")
    int insertRoomDevice(String name, String device);

    @Insert("insert into room_user values (null,#{name},#{user})")
    int insertRoomUser(String name, String user);

    @Delete("delete from room_device where name=#{name} and devid=#{device}")
    int deleteRoomDevice(String name, String device);

    @Delete("delete from room_user where name=#{name} and user=#{user}")
    int deleteRoomUser(String name, String user);

    @Select("select * from user WHERE username NOT IN (SELECT user FROM room_user)")
    List<Map<String, Object>> selectAllUnbindRoomUser();

    @Select("select * from device WHERE name NOT IN (SELECT devid FROM room_device)")
    List<Map<String, Object>> selectAllUnbindRoomDevice();

    @Select("SELECT ru.name AS name, rd.devid FROM room_device rd JOIN room_user ru ON rd.name = ru.name WHERE ru.user = #{userName}")
    List<Map<String,Object>> selectRoomAndDeviceByUser(String userName);
}
